<template>
  <div class="attachment-page">
    <el-row class="main-layout" :gutter="10">
      <!-- 左侧：所有附件 -->
      <el-col :span="12" class="left-panel">
        <el-card shadow="always" class="attachment-card">
          <!-- 搜索栏 -->
          <div class="search-bar">
            <el-form :inline="true" :model="queryParams" size="small" label-width="60px" @submit.native.prevent>
              <el-form-item label="标题">
                <el-input
                  v-model="queryParams.title"
                  placeholder="请输入标题"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
          <!-- 所有附件表格 -->
          <el-table
            class="attachment-table"
            :data="projectAttachmentList"
            v-loading="loading"
            @selection-change="handleSelectionChange"
            @row-click="showFile"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column label="附件ID" prop="attachmentId" />
            <el-table-column label="标题" prop="title" />
            <el-table-column label="创建人" prop="createdBy">
              <template v-slot="scope">
                <user-and-avatar :userId="scope.row.createdBy" />
              </template>
            </el-table-column>
            <el-table-column align="center" label="创建时间" prop="createdAt" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total > 0"
            :limit.sync="queryParams.pageSize"
            :page.sync="queryParams.pageNum"
            :total="total"
            @pagination="getList"
          />
        </el-card>
      </el-col>

      <!-- 右侧：我的附件 -->
      <el-col :span="12" class="right-panel">
        <el-card shadow="always" class="attachment-card">
          <div class="search-bar">
            <el-form :inline="true" :model="myQueryParams" size="small" label-width="60px" @submit.native.prevent>
              <el-form-item label="标题">
                <el-input
                  v-model="myQueryParams.title"
                  placeholder="请输入标题"
                  clearable
                  @keyup.enter.native="myHandleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="myHandleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="myResetQuery">重置</el-button>
              </el-form-item>
            </el-form>
            <div class="right-button">
              <el-button size="small" type="primary" icon="el-icon-plus" @click="handleAdd">新增附件</el-button>
            </div>
          </div>

          <el-table
            class="attachment-table"
            :data="myAttachmentList"
            v-loading="myLoading"
            @row-click="showFile"
          >
            <el-table-column label="标题" prop="title" />
            <el-table-column align="center" label="创建时间" prop="createdAt" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="更新时间" prop="updatedAt" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column width="200px" align="center" class-name="small-padding fixed-width" label="操作">
              <template slot-scope="scope">
                <el-button
                  icon="el-icon-edit"
                  size="mini"
                  type="text"
                  @click.stop="handleUpdate(scope.row)"
                >修改</el-button>
                <el-button
                  icon="el-icon-delete"
                  size="mini"
                  type="text"
                  @click.stop="handleDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="myTotal > 0"
            :limit.sync="myQueryParams.pageSize"
            :page.sync="myQueryParams.pageNum"
            :total="myTotal"
            @pagination="getMyList"
          />
        </el-card>
      </el-col>
    </el-row>

    <!-- 添加/修改附件弹框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="文件" prop="fileUrl">
          <file-upload v-model="form.fileUrl" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 文件预览弹框 -->
    <el-dialog :visible.sync="fileShowOk" width="60%">
      <file-show v-if="fileShowOk" :url="fileUrl" />
    </el-dialog>
  </div>
</template>
<script>
import {
  addProjectAttachment,
  delProjectAttachment,
  getProjectAttachment,
  listMyProjectAttachment,
  listProjectAttachment,
  updateProjectAttachment
} from "@/api/projectUser/userProjectAttachment";
import FileShow from "@/components/FileShow/index.vue";
import UserAndAvatar from "@/components/UserAndAvatar/index.vue";

export default {
  name: "ProjectAttachment",
  components: {UserAndAvatar, FileShow},
  dicts: ['review_status'],
  data() {
    return {
      myLoading:true,
      fileShowOk: false,
      fileUrl: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      myTotal:0,
      // 项目附件表格数据
      projectAttachmentList: [],
      myAttachmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        title: null,
        reviewStatus: null,
        reviewedBy: null,
      },
      myQueryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        title: null,
        reviewStatus: null,
        reviewedBy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"}
        ],
        fileUrl: [
          {required: true, message: "文件不能为空", trigger: "blur"}
        ],
        reviewStatus: [
          {required: true, message: "附件审核状态不能为空", trigger: "change"}
        ]
      }
    };
  },
  props: {
    projectId: {
      type: [String, Number],
    }
  },
  created() {
    this.getList();
    this.getMyList();
  },
  methods: {
    /** 查看文件 */
    showFile(row) {
      this.fileUrl = row.fileUrl
      this.fileShowOk = true
    },
    /** 查询项目附件列表 */
    getList() {
      this.loading = true;
      listProjectAttachment(this.projectId, this.queryParams).then(response => {
        this.projectAttachmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getMyList() {
      this.myLoading = true;
      listMyProjectAttachment(this.projectId, this.myQueryParams).then(response => {
        this.myAttachmentList = response.rows;
        this.myTotal = response.total;
        this.myLoading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        attachmentId: null,
        projectId: null,
        title: null,
        fileUrl: null,
        reviewStatus: null,
        reviewedBy: null,
        createdAt: null,
        createdBy: null,
        updatedAt: null,
        updatedBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    myHandleQuery() {
      this.queryParams.pageNum = 1;
      this.getMyList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    myResetQuery() {
      this.myQueryParams={
        pageNum: 1,
          pageSize: 10,
          projectId: null,
          title: null,
          reviewStatus: null,
          reviewedBy: null,
      },
      this.resetForm("queryForm");
      this.myHandleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.attachmentId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目附件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const attachmentId = row.attachmentId || this.ids
      getProjectAttachment(attachmentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目附件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.attachmentId != null) {
            updateProjectAttachment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getMyList();
            });
          } else {
            addProjectAttachment(this.projectId, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getMyList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const attachmentIds = row.attachmentId || this.ids;
      this.$modal.confirm('是否确认删除项目附件编号为"' + attachmentIds + '"的数据项？').then(function () {
        return delProjectAttachment(attachmentIds);
      }).then(() => {
        this.getList();
        this.getMyList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/projectAttachment/export', {
        ...this.queryParams
      }, `projectAttachment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.attachment-page {
  height: 80vh;
  box-sizing: border-box;
  overflow: hidden;
}

.main-layout {
  height: 100%;
}

.left-panel,
.right-panel {
  height: 100%;
  display: flex;

}

.attachment-card {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 0;

}

.search-bar,
.top-bar {
  height: 50px;

  display: flex;

}

.right-button {
  margin-left: auto;
}

.attachment-table {
  flex: 1;
  overflow: auto;
  padding: 0 20px 20px;
}
</style>
